package com.guyu.common.core.entity.page;

import com.guyu.common.core.enums.BooleanEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * ID分页对象
 */
@Getter
@NoArgsConstructor
public class CursorRequest<T> {

    /**
     * 分页游标
     */
    @Setter
    private T cursor;
    /**
     * 每页显示数量[1,1000]
     */
    private long pageSize = 10;
    /**
     * 分页方向 1：下一页，0：上一页
     */
    private int action = 1;

    /**
     * 游标分页
     *
     * @param cursor 游标
     * @param action 1：下一页，0：上一页
     */
    public CursorRequest(T cursor, int action) {
        this(cursor, action, 10L);
    }

    public CursorRequest(T cursor, int action, long pageSize) {
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than 1!");
        }
        if (pageSize > 1000) {
            throw new IllegalArgumentException("Page size must not be greater than 1000!");
        }
        if (action != 0 && action != 1) {
            throw new IllegalArgumentException("direction values only 0 and 1");
        }
        this.cursor = cursor;
        this.pageSize = pageSize;
        this.action = action;
    }

    /**
     * 上一页
     *
     * @param cursor   当前页第一条的游标
     * @param pageSize 每页显示数量[1,1000]
     * @return 分页对象
     */
    public static <T> CursorRequest previous(T cursor, long pageSize) {
        return new CursorRequest(cursor, 0, pageSize);
    }

    /**
     * 下一页
     *
     * @param cursor   当前页最后一条的游标
     * @param pageSize 每页显示数量[1,1000]
     * @return 分页对象
     */
    public static <T> CursorRequest next(T cursor, long pageSize) {
        return new CursorRequest(cursor, 1, pageSize);
    }

    public void setPageSize(long pageSize) {
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than 1!");
        }
        if (pageSize > 1000) {
            throw new IllegalArgumentException("Page size must not be greater than 1000!");
        }
        this.pageSize = pageSize;
    }

    public void setAction(int action) {
        if (action != 0 && action != 1) {
            throw new IllegalArgumentException("direction values only 0 and 1");
        }
        this.action = action;
    }

    /**
     * 是否下一页
     */
    public boolean hasNext() {
        return BooleanEnum.isYes(action);
    }

}
